// pages/workorderlist/workorderlist.js
// display: block;
// font - size: 28rpx;
// color: #000000;
// line - height: 40rpx;
// height: 120rpx;
// overflow: hidden;
// white - space: nowrap;
// text - overflow: ellipsis;
const httprequest = require('../../utils/http_utils.js')
const dateutil = require('../../utils/date_util.js')
const common = require('../../utils/common.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    navScrollLeft: 0,
    navData: [{
        text: '未转交' //1
      },
      {
        text: '未分配' //6
      },
      {
        text: '未接受' //5
      },
      {
        text: '处理中' //3
      },
      {
        text: '审核中' //11
      },
      {
        text: '已完成' //4
      },
      {
        text: '已撤销' //9
      },
      {
        text: '已关闭' //10
      }
    ],
    pageSize: 10,
    pageNum: 1,
    state: 1,
    WorkOrderList: [],
    showzanwu: false, //是否显示暂无图片
    windowWidth: 0,
    windowHeight: 0,
    touch: true,
    totalNum: 0, //总条数
    uploading: false,
    downloading: false,
    noMore: false,
    loadingFailed: false,
    title: '工单任务',
    isshoworder: false, //默认跳转到上一页
  },

  click: function(e) {
    wx.navigateTo({
      url: '../details/details?RowGuid=' + e.currentTarget.dataset.rowguid,
    })
  },

  switchNav(event) {
    var cur = event.currentTarget.dataset.current;
    //每个tab选项宽度占1/5
    console.log("windowWidth----" + this.data.windowWidth)
    var singleNavWidth = this.data.windowWidth / 5;
    //tab选项居中                            
    this.setData({
      navScrollLeft: (cur - 2) * singleNavWidth
    })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
    if (cur == 0) {
      this.setData({
        state: 1
      })
    } else if (cur == 1) {
      this.setData({
        state: 6
      })
    } else if (cur == 2) {
      this.setData({
        state: 5
      })
    } else if (cur == 3) {
      this.setData({
        state: 3
      })
    } else if (cur == 4) {
      this.setData({
        state: 11
      })
    } else if (cur == 5) {
      this.setData({
        state: 4
      })
    } else if (cur == 6) {
      this.setData({
        state: 9
      })
    } else if (cur == 7) {
      this.setData({
        state: 10
      })
    }
  },

  switchTab(event) {
    var cur = event.detail.current;
    // console.log(event.detail.source + '----touch')
    var singleNavWidth = this.data.windowWidth / 5;
    this.setData({
      currentTab: cur,
      navScrollLeft: (cur - 2) * singleNavWidth
    });
    if (cur == 0) {
      this.setData({
        state: 1
      })
    } else if (cur == 1) {
      this.setData({
        state: 6
      })
    } else if (cur == 2) {
      this.setData({
        state: 5
      })
    } else if (cur == 3) {
      this.setData({
        state: 3
      })
    } else if (cur == 4) {
      this.setData({
        state: 11
      })
    } else if (cur == 5) {
      this.setData({
        state: 4
      })
    } else if (cur == 6) {
      this.setData({
        state: 9
      })
    } else if (cur == 7) {
      this.setData({
        state: 10
      })
    }
    this.getInitWorkOrderlist();
  },

  //获取工单列表
  getWorkOrderList: function() {
    var that = this
    var params = {
      PageSize: that.data.pageSize,
      CurrentPage: that.data.pageNum,
      param: that.data.state
    };
    httprequest.GetValidateRequest(httprequest.apiInfo.Project_GetWorkOrderSearchApi, params,
      function(res) {
        if (res.totalNum == 0) {
          that.setData({
            showzanwu: true
          })
        } else {
          that.setData({
            showzanwu: false,
            totalNum: res.totalNum
          })
          var datalist = that.data.WorkOrderList
          console.log("datalist", JSON.stringify(datalist))
          //过滤时间
          for (var i = 0; i < res.pageData.length; i++) {
            var param = {
              WorkOrderId: res.pageData[i].WorkOrderId,
              Description: res.pageData[i].Description,
              OperateDate: dateutil.formatTime(res.pageData[i].OperateDate),
              WorkType: common.getDicText('工单类型', res.pageData[i].WorkType),
              RowGuid: res.pageData[i].RowGuid,
            }
            datalist.push(param)
          }
          console.log("newlist", JSON.stringify(datalist))
          that.setData({
            WorkOrderList: datalist
          })
        }
        that.setData({
          uploading: false,
          downloading: false,
          noMore: false
        })
      },
      function(err) {
        console.error(err)
        that.setData({
          showzanwu: true
        })
      }
    )
  },

  //初始化列表
  getInitWorkOrderlist: function() {
    var that = this
    this.setData({
      WorkOrderList: []
    })
    var params = {
      PageSize: that.data.pageSize,
      CurrentPage: that.data.pageNum,
      param: that.data.state
    };
    httprequest.GetValidateRequest(httprequest.apiInfo.Project_GetWorkOrderSearchApi, params,
      function(res) {
        if (res.totalNum == 0) {
          that.setData({
            showzanwu: true
          })
        } else {
          that.setData({
            showzanwu: false,
            totalNum: res.totalNum
          })
          var datalist = new Array();
          console.log("datalist", JSON.stringify(datalist))
          //过滤时间
          for (var i = 0; i < res.pageData.length; i++) {
            var param = {
              WorkOrderId: res.pageData[i].WorkOrderId,
              Description: res.pageData[i].Description,
              OperateDate: dateutil.formatTime(res.pageData[i].OperateDate),
              WorkType: common.getDicText('工单类型', res.pageData[i].WorkType),
              RowGuid: res.pageData[i].RowGuid,
            }
            datalist.push(param)
          }
          console.log("newlist", JSON.stringify(datalist))
          that.setData({
            WorkOrderList: datalist
          })
        }
        that.setData({
          uploading: false,
          downloading: false,
          noMore: false
        })
      },
      function(err) {
        console.error(err)
        that.setData({
          showzanwu: true
        })
      }
    )
  },

  getSystemInfo: function() {
    var that = this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          windowWidth: res.windowWidth,
          windowHeight: res.windowHeight
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  //下拉
  scrollToLower: function() {
    console.log("scrollToLower")
    var that = this
    var pages = Math.round((that.data.totalNum / that.data.pageSize) * 100) / 100
    if (that.data.pageNum > pages && pages != 0) {
      that.setData({
        noMore: true
      })
    } else {
      that.setData({
        noMore: false,
        downloading: true,
        pageNum: that.data.pageNum + 1
      })
      that.getWorkOrderList();
    }
  },

  //上拉
  // scrollToUpper: function() {
  //   console.log("scrollToUpper")
  //   var that = this
  //   that.setData({
  //     uploading: true,
  //     noMore: false,
  //     pageNum: 1,
  //     WorkOrderList: []
  //   })
  //   that.getWorkOrderList();
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.currentTab != undefined && options.state != undefined && options.currentTab != '' && options.state != '') {
      this.setData({
        currentTab: parseInt(options.currentTab),
        state: parseInt(options.state),
        isshoworder: options.isshoworder,
        WorkOrderList: []
      })
    }
    
    this.getSystemInfo();
    this.refreshView = this.selectComponent("#refreshView")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    console.log(this.data.isshoworder)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getInitWorkOrderlist();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  //触摸开始
  handletouchstart: function (event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function (event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function (event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function (event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function (event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => { 
      console.log("scrollToUpper")
      var that = this
      that.setData({
        uploading: true,
        noMore: false,
        pageNum: 1,
        WorkOrderList: []
      })
      that.getWorkOrderList();
      this.refreshView.stopPullRefresh() 
      }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})